package com.qunar.mina.app.utils;

import java.util.Date;

import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.DateTools.Resolution;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.util.NumericUtils;

import com.qunar.mina.app.config.AnalyzerConfig;
import com.qunar.mina.app.config.DirectoryConfig;
import com.qunar.wireless.ugc.qcfclient.dto.Address;
import com.qunar.wireless.ugc.qcfclient.dto.Auditor;
import com.qunar.wireless.ugc.qcfclient.dto.Pic;
import com.qunar.wireless.ugc.qcfclient.dto.User;

public class WriterUtils {
	private static IndexWriter fsAddressWriter;
	private static IndexWriter fsUserWriter;
	private static IndexWriter fsAuditorWriter;
	private static IndexWriter fsPicWriter;

	static {
		try {
			fsAddressWriter = new IndexWriter(DirectoryConfig.getFsAddressDirectory(),
					AnalyzerConfig.getAnalyzer(), MaxFieldLength.LIMITED);
			
			fsUserWriter = new IndexWriter(DirectoryConfig.getFSUserDirectory(),
					AnalyzerConfig.getAnalyzer(), MaxFieldLength.LIMITED);
			
			fsAuditorWriter = new IndexWriter(DirectoryConfig.getFSAuditorDirectory(),
					AnalyzerConfig.getAnalyzer(), MaxFieldLength.LIMITED);
			
			fsPicWriter = new IndexWriter(DirectoryConfig.getFSPicDirectory(),
					AnalyzerConfig.getAnalyzer(), MaxFieldLength.LIMITED);
			
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		try {
			Runtime.getRuntime().addShutdownHook(new Thread() {
				@Override
				public void run() {
					if (fsAddressWriter != null) {
						try {
							fsAddressWriter.close();
						} catch (Exception e) {
							throw new RuntimeException(e);
						}
					}
					
					if (fsUserWriter != null) {
						try {
							fsUserWriter.close();
						} catch (Exception e) {
							throw new RuntimeException(e);
						}
					}
					
					if (fsAuditorWriter != null) {
						try {
							fsAuditorWriter.close();
						} catch (Exception e) {
							throw new RuntimeException(e);
						}
					}
					
					if (fsPicWriter != null) {
						try {
							fsPicWriter.close();
						} catch (Exception e) {
							throw new RuntimeException(e);
						}
					}
					
				}
			});
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public static IndexWriter getFsAddressWriter() {
		return fsAddressWriter;
	}
	
	public static IndexWriter getUserWriter() {
		return fsUserWriter;
	}
	
	public static IndexWriter getAuditorWriter() {
		return fsAuditorWriter;
	}
	
	public static IndexWriter getPicWriter() {
		return fsPicWriter;
	}
	
	/**
	 * 将address对象转换document
	 */
	public static Document address2Document(Address address) {
		Document document = new Document();
		document.add(new Field("addressId", NumericUtils.longToPrefixCoded(address.getAddressId()), Store.YES, Index.NOT_ANALYZED));
		if (address.getName() != null && !"".equals(address.getName()))
			document.add(new Field("name", address.getName(), Store.YES, Index.ANALYZED));
		if (address.getDetail() != null && !"".equals(address.getDetail()))
			document.add(new Field("detail", address.getDetail(), Store.YES, Index.ANALYZED));
		if (address.getCityId() != null)
			document.add(new Field("cityId", address.getCityId().toString(), Store.YES, Index.NOT_ANALYZED));
		if (address.getIsValid() != null)
			document.add(new Field("isValid", address.getIsValid().toString(), Store.YES, Index.NOT_ANALYZED));
		if (address.getIsShield() != null)
			document.add(new Field("isShield", address.getIsShield().toString(), Store.YES, Index.NOT_ANALYZED));
		return document;
	}
	
	
	/**
	 * 将user对象转换document
	 */
	public static Document user2Document(User user) {
		Document document = new Document();
		document.add(new Field("userId", NumericUtils.longToPrefixCoded(user.getUserId()), Store.YES, Index.NOT_ANALYZED));
		if (user.getUsername() != null && !"".equals(user.getUsername()))
			document.add(new Field("username",user.getUsername(), Store.YES, Index.ANALYZED));
		
		return document;
	}
	
	/**
	 * 将auditor对象转换为document
	 * @param auditor
	 * @return
	 */
	public static Document auditor2Document(Auditor auditor) {
		Document document = new Document();
		document.add(new Field("auditorId", NumericUtils.intToPrefixCoded(auditor.getAuditorId()), Store.YES, Index.NOT_ANALYZED));
		if (auditor.getName() != null && !"".equals(auditor.getName()))
			document.add(new Field("name", auditor.getName(), Store.YES, Index.ANALYZED));
		
		if (auditor.getRtxAccount() != null && !"".equals(auditor.getRtxAccount()))
			document.add(new Field("rtxAccount", auditor.getRtxAccount(), Store.YES, Index.ANALYZED));
		
		return document;
		
	}
	
	/**
	 * 将pic对象转换为Document
	 * @param pic
	 * @return
	 */
	public static Document pic2Document(Pic pic) {
		Document document = new Document();
		document.add(new Field("picId", NumericUtils.longToPrefixCoded(pic.getPicId()), Store.YES, Index.NOT_ANALYZED));
		if(pic.getUserId() != 0) {
			document.add(new Field("userId", NumericUtils.longToPrefixCoded(pic.getUserId()), Store.YES, Index.NOT_ANALYZED));
		}
		
		if(pic.getAuditorId() != 0) {
			document.add(new Field("auditorId", NumericUtils.intToPrefixCoded(pic.getAuditorId()), Store.YES, Index.NOT_ANALYZED));
		}
		
		if(pic.getUrl() != null && !"".equals(pic.getUrl())) {
			document.add(new Field("url", pic.getUrl(), Store.YES, Index.NOT_ANALYZED));
		}
		
		if(pic.getDescription() != null && !"".equals(pic.getDescription())) {
			document.add(new Field("description", pic.getDescription(), Store.YES, Index.ANALYZED));
		}
		
		if(pic.getCreateTime() != null) {
			document.add(new Field("createTime", DateTools.dateToString(pic.getCreateTime(), Resolution.SECOND), Store.YES, Index.NOT_ANALYZED));
		}
		
		if(pic.getAuditTime() != null) {
			Date auditTime = pic.getAuditTime();
			document.add(new Field("auditTime", DateTools.dateToString(auditTime, Resolution.SECOND), Store.YES, Index.NOT_ANALYZED));
		}
		
		if(pic.getActivityName() != null && !"".equals(pic.getActivityName())) {
			document.add(new Field("activityName", pic.getActivityName(), Store.YES, Index.ANALYZED));
		}
		
		if(pic.getIllegalityStatus() != 0) {
			document.add(new Field("illegalityStatus", String.valueOf(pic.getIllegalityStatus()), Store.YES, Index.NOT_ANALYZED));
		}
		
		return document;
	}

}
